package com.a85.wxhelper.bean;

import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;

import java.util.List;

/**
 * @author hejiangwei
 * Created at 2018/8/13.
 */
public class InstalledModule {
    private static final int FLAG_FORWARD_LOCK = 1 << 29;
    public final String packageName;
    public final boolean isFramework;
    public final String versionName;
    public final int versionCode;
    public final int minVersion;
    public ApplicationInfo app;
    private String appName; // loaded lazyily
    private String description; // loaded lazyily

    private Drawable.ConstantState iconCache = null;

    public InstalledModule(PackageInfo pkg, boolean isFramework) {
        this.app = pkg.applicationInfo;
        this.packageName = pkg.packageName;
        this.isFramework = isFramework;
        this.versionName = pkg.versionName;
        this.versionCode = pkg.versionCode;

        if (isFramework) {
            this.minVersion = 0;
            this.description = "";
        } else {
            Object minVersionRaw = app.metaData.get("xposedminversion");
            if (minVersionRaw instanceof Integer) {
                this.minVersion = (Integer) minVersionRaw;
            } else if (minVersionRaw instanceof String) {
                this.minVersion = extractIntPart((String) minVersionRaw);
            } else {
                this.minVersion = 0;
            }
        }
    }
    public static int extractIntPart(String str) {
        int result = 0, length = str.length();
        for (int offset = 0; offset < length; offset++) {
            char c = str.charAt(offset);
            if ('0' <= c && c <= '9') {
                result = result * 10 + (c - '0');
            } else {
                break;
            }
        }
        return result;
    }
    public boolean isInstalledOnExternalStorage() {
        return (app.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0;
    }

    /**
     * @hide
     */
    public boolean isForwardLocked() {
        return (app.flags & FLAG_FORWARD_LOCK) != 0;
    }

    public String getAppName(PackageManager mPm) {
        if (appName == null) {
            appName = app.loadLabel(mPm).toString();
        }
        return appName;
    }

    public String getDescription(PackageManager mPm) {
        if (this.description == null) {
            Object descriptionRaw = app.metaData.get("xposeddescription");
            String descriptionTmp = null;
            if (descriptionRaw instanceof String) {
                descriptionTmp = ((String) descriptionRaw).trim();
            } else if (descriptionRaw instanceof Integer) {
                try {
                    int resId = (Integer) descriptionRaw;
                    if (resId != 0) {
                        descriptionTmp = mPm.getResourcesForApplication(app).getString(resId).trim();
                    }
                } catch (Exception ignored) {
                }
            }
            this.description = (descriptionTmp != null) ? descriptionTmp : "";
        }
        return this.description;
    }


}
